package com.sy.simpleegg.room.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import com.sy.simpleegg.platform.BackstagePlatform
import com.sy.simpleegg.room.entity.GoodEntity

@Dao
interface GoodDao {
    @Query("SELECT * FROM tb_good")
    fun getAll(): MutableList<GoodEntity>?

    @Query("SELECT * FROM tb_good WHERE platform = :platform ORDER BY id DESC LIMIT 1")
    fun getLastGoods(platform: BackstagePlatform): MutableList<GoodEntity>?
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(goodEntity: GoodEntity)

    @Transaction
    fun insertAll(goodEntities: MutableList<GoodEntity>){
        goodEntities.forEach {
            insert(it)
        }
    }

    @Delete
    fun delete(goodEntity: GoodEntity)

    @Query("DELETE FROM tb_good")
    fun deleteAll()
}